package com.delos.hello.controller;

import com.delos.hello.rpc.IHelloRpc;
import com.delos.world.rpc.IWorldRpc;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author zouyaowen
 * @date 2020-04
 */
@RestController
@Slf4j
public class HelloController {

    /**
     * 避免启动检查设置check为false
     * (retries = 0, check = false)
     */
    @Reference
    private IWorldRpc worldRpc;
    /**
     * 远程接口本地可以调用，使用Autowire注解idea编辑器提示异常，但是运行正常,Resource注解编辑器不提示异常
     * 本地调用本地提供的远程接口意义不大
     */
    @Resource
    private IHelloRpc helloRpc;

    @GetMapping("/hello")
    public String hello(String name) {
        String helloLocal = helloRpc.hello("hello项目本地调用远程接口");
        log.info("hello项目本地调用远程接口返回结果=[{}]", helloLocal);
        String worldRes = worldRpc.world(name);
        log.info("world远程接口远程调用返回=[{}]", worldRes);
        CompletableFuture<String> completableFuture = worldRpc.asyncWorld(name);
        log.info("world远程接口异步调用完成");
        completableFuture.thenAccept(System.out::println);
        return "hello" + name;
    }

}
